package com.cskt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskt.common.condition.UserRegisterCondition;
import com.cskt.common.constants.ErrorCodeEnum;
import com.cskt.common.vo.ReturnResult;
import com.cskt.entity.User;
import com.cskt.service.UserService;
import com.google.gson.internal.$Gson$Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.regex.Pattern;

@RestController
@Api(tags = "用户控制器")
@RequestMapping("/api")
@Slf4j
public class UserController {

    @Resource
    UserService UserService;
    @ApiOperation(value = "检查用户名是否已存在",httpMethod ="GET",protocols = "HTTP",produces = MediaType.APPLICATION_JSON_VALUE,response =ReturnResult.class,notes = "验证是否已经存在该用户名")
    @RequestMapping(value = "/ckusr",method = RequestMethod.GET)
    public ReturnResult checkUser(@ApiParam(name = "name",value = "待检验用户名",defaultValue = "test@bdqn.cn") @RequestParam String name){
        //StringUtils方法
        if(StringUtils.isEmpty(name)){
            //参数为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_PARAMETER_IS_EMPTY);

        }
        //判断用户是否存在
        //条件构造器
        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda().eq(User::getUserCode, name);
        User user = UserService.getOne(queryWrapper);

        if (user !=null){
            //当用户数据不为空的时候，校验不通过
            return ReturnResult.error(ErrorCodeEnum.AUTH_USER_ALREADY_EXISTS);
        }
        return ReturnResult.ok();
    }
    //邮箱注册
    @RequestMapping(value = "/doregister",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ReturnResult doRegister(@RequestBody UserRegisterCondition condition){
        if (this.validEmail(condition.getUserCode())) {
            return ReturnResult.error(ErrorCodeEnum.AUTH_ILLEGAL_USERCODE);
        }

        boolean registerResult = UserService.userRegister(condition,"mail");
        if (registerResult) {
            return ReturnResult.ok();
        }
        return ReturnResult.error();
    }
    @RequestMapping(value = "activateByPhone",method = RequestMethod.POST)
    public ReturnResult activePhone(@RequestBody UserRegisterCondition condition){
        if (!this.validPhone(condition.getUserCode())) {
            return ReturnResult.error(ErrorCodeEnum.AUTH_ILLEGAL_USERCODE);
        }
        boolean registerResult = UserService.userRegister(condition,"phone");
        if (registerResult) {
            return ReturnResult.ok();
        }
        return ReturnResult.error();
    }


    /**
     * 通过正则表达式校验邮箱地址是否符合要求
     * 合法E-mail地址：
     * 1. 必须包含一个并且只有一个符号“@”
     * 2. 第一个字符不得是“@”或者“.”
     * 3. 不允许出现“@.”或者.@
     * 4. 结尾不得是字符“@”或者“.”
     * 5. 允许“@”前的字符中出现“＋”
     * 6. 不允许“＋”在最前面，或者“＋@”
     * @param email
     * @return
     */
    private boolean validEmail(String email){
        String regex="/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\\.[a-z]{2,}$/\n" ;
        return Pattern.compile(regex).matcher(email).find();
    }
    /**
     * 验证手机号码的格式是否正确
     * @param phone 手机号码
     * @return 返回true表示手机号码验证通过。否则返回false
     */
    public boolean validPhone(String phone) {
        String regex = "^1[3|4|5|7|8][0-9]{9}$";
        return Pattern.compile(regex).matcher(phone).find();
    }

    /**
     * 根据用户手机号跟验证码激活用户账号
     * @param phone
     * @param code
     * @return
     */
    @RequestMapping(value = "activateByPhone",method = RequestMethod.PUT)
    public ReturnResult activeByPhone(@RequestParam("user") String phone,@RequestParam String code){
    boolean registerResult = UserService.validatePhone(phone, code);
    if (registerResult) {
        return ReturnResult.ok();
    }
    return ReturnResult.error(ErrorCodeEnum.AUTH_ACTIVATE_FAILED);
}

}


